<?php
import( 'de.ceus-media.framework.krypton.core.DefinitionView' );
import( 'classes.view.panel.EnvironmentSelector' );
import( 'classes.model.Topic' );
import( 'classes.model.Case' );
import( 'classes.logic.Topic' );
/**
 *	Option Views.
 *	@extends		Core_View
 *	@uses			View_Panel_PlatformSelector
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
/**
 *	Option Views.
 *	@extends		Core_View
 *	@uses			View_Panel_PlatformSelector
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
class View_Management_Structure extends Framework_Krypton_Core_DefinitionView
{
	private $nodeId;

	/**
	 *	Builds Car Views.
	 *	@access		public
	 *	@return		string
	 */
	public function buildContent()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		

		$selector	= new View_Panel_EnvironmentSelector();
		$selector->setProjects( true );
		$ui['selector'] = $selector->buildContent();

		$projectId	= $session->get( 'projectId' );
		$topicId	= $request->get( 'topicId' );
		$caseId		= $request->get( 'caseId' );

		if( $projectId )
		{
			$ui['tree']		= $this->buildContentTopicTree( $projectId, $topicId );
			if( $topicId && $caseId )
			{
				$ui['content']	= $this->buildContentCaseEdit( $projectId, $topicId, $caseId );
				$ui['content']	.= $this->buildContentCaseAdd( $projectId, $topicId );
			}
			else if( $topicId )
			{
				$ui['content']	= $this->buildContentTopicEdit( $projectId, $topicId );
				$ui['content']	.= $this->buildContentTopicAdd( $projectId, $topicId );
				$ui['content']	.= $this->buildContentCaseAdd( $projectId, $topicId );
			}
			$content	= $this->loadTemplate( 'management.structure.content', $ui );
		}
		else
		{
			$content	= $ui['selector'].$this->loadContent( 'management.structure.home.html' );
		}
		return $content;
	}

	private function buildContentCaseAdd( $projectId, $topicId )
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['structure']['addCase'];
		
	/*	$logic	= new Logic_Topic();
		$parent	= $logic->getTopicData( $topicId );
		$data['topic']	= $parent['title'];
	*/
		$opt_topic	= array( 0 => "" );
		foreach( Logic_Topic::getTopicList( $projectId, 0, 0, "- " ) as $id => $title )
			$opt_topic[$id]	= $title;
		$opt_topic['_selected']	= $topicId;
		$sources	= array( 'opt_topic' => $opt_topic );
		$data['priority']	= 3;

		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'management/structure', 'addCase', 'structure', 'addCase', $data, $sources );
		$button_add		= UI_HTML_Elements::Button( 'addCase', $words['button_add'] );
		$ui['field_button']	= UI_HTML_Elements::Field( '', $button_add );

		$ui['form']		= UI_HTML_Elements::Form( 'addCase', "?link=".$request->get( 'link' )."&topicId=".$topicId );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'add' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'management.structure.case.add', $ui );
	}

	private function buildContentTopicAdd( $projectId, $topicId )
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['structure']['addTopic'];

		$opt_parent	= array( 0 => "" );
		foreach( Logic_Topic::getTopicList( $projectId, 0, 0, "- " ) as $id => $title )
			$opt_parent[$id]	= $title;
		$opt_parent['_selected']	= $topicId;
		$sources	= array( 'opt_parent' => $opt_parent );

		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'management/structure', 'addTopic', 'structure', 'addTopic', array(), $sources );
		$button_add		= UI_HTML_Elements::Button( 'addTopic', $words['button_add'] );
		$ui['field_button']	= UI_HTML_Elements::Field( '', $button_add );

		$ui['form']		= UI_HTML_Elements::Form( 'addTopic', "?link=".$request->get( 'link' )."&topicId=".$topicId );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'add' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'management.structure.topic.add', $ui );
	}

	private function buildContentCaseEdit( $projectId, $topicId, $caseId )
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['structure']['editCase'];

		$data	= Logic_Case::getCaseData( $caseId );
		$opt_topic	= array_merge( array( 0 => "" ), Logic_Topic::getTopicList( $projectId, 0, 0, "- " ) );
		$opt_topic['_selected']	= $data['topic_id'];
		$sources	= array( 'opt_topic' => $opt_topic );

//		$data['author']		= $logic->getUserName( $data['author_id'] );
		$data['author']		= Logic_Authentication::getUsernameFromUid( $data['author_id'] );
		$data['created']	= $data['created'] ? date( "d.m.Y H:i", $data['created'] ) : "";
		$data['modified']	= $data['modified'] ? date( "d.m.Y H:i", $data['modified'] ) : "";

		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'management/structure', 'editCase', 'structure', 'editCase', $data, $sources );
		$button_edit	= UI_HTML_Elements::Button( 'editCase', $words['button_edit'] );
		$ui['field_button']	= UI_HTML_Elements::Field( '', $button_edit );

		$ui['form']		= UI_HTML_Elements::Form( 'editCase', "?link=".$request->get( 'link' )."&topicId=".$topicId."&caseId=".$caseId );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'edit' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'management.structure.case.edit', $ui );
	}
	
	private function buildContentTopicEdit( $projectId, $topicId )
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['structure']['editTopic'];

		$data	= Logic_Topic::getTopicData( $topicId );
		$opt_parent	= array_merge( array( 0 => "" ), Logic_Topic::getTopicList( $projectId, 0, 0, "- " ) );
		$opt_parent['_selected']	= $data['parent_id'];
		$sources	= array( 'opt_parent' => $opt_parent );

		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'management/structure', 'editTopic', 'structure', 'editTopic', $data, $sources );
		$button_edit	= UI_HTML_Elements::Button( 'editTopic', $words['button_edit'] );
		$ui['field_button']	= UI_HTML_Elements::Field( '', $button_edit );

		$ui['form']		= UI_HTML_Elements::Form( 'editTopic', "?link=".$request->get( 'link' )."&topicId=".$topicId );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'edit' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'management.structure.topic.edit', $ui );
	}
	
	private function buildContentTopicTree( $projectId, $topicId = 0 )
	{
		$config			= Framework_Krypton_Core_Registry::getStatic( 'config' );
		$request		= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$words			= $this->words['structure']['tree'];
		$currentLink	= $request->get( 'link' );
		$files			= array();

		$model	= new Model_Topic();

//		$st	= new Framework_Krypton_Core_StopWatch();
		//  --  CHECK CACHE  --  //
		$cached		= false;
		$cacheFile	= $config['paths']['cache']."optionTreeAdmin_".$projectId.".cache";
		if( file_exists( $cacheFile ) )
		{
			$conditions	= array( 'project_id' => $projectId, 'modified' => ">=".filemtime( $cacheFile ) );
			$cached	= !(bool)$model->find( array( 'topic_id' ), $conditions );
		}
		if( $cached )
			$tree	= file_get_contents( $cacheFile );
		else
		{
	/*		$model	= new Model_Topic();
			$model->focusIndex( 'parent_id', 0 );
			$model->focusIndex( 'project_id', $projectId );
			$model->get();
	*/		
			$tree	= $this->constructTree( $projectId, $currentLink, 0, false, 0, array( 'id' => 'gray' ) );
			if( $tree )
				file_put_contents( $cacheFile, $tree );
		}
//		remark( $st->stop() ) ;
		
		$ui['tree']		= $tree;
		$ui['heading']	= $words['heading'];
		return $this->loadTemplate( 'management.structure.tree', $ui );

	}


	private function constructTree( $projectId, $currentLink, $parentId, $parentPath, $level, $attributes = array() )
	{
		$list	= array();
		
		//  --  TEST CASES  --  //
		$model	= new Model_Case();
		$model->focusIndex( 'topic_id', $parentId );
		$cases	= $model->get( false );
		foreach( $cases as $case )
		{
			$path		= $parentPath . ( $parentPath ? ".".$case['case_id'] : $case['case_id'] );
			$link		= UI_HTML_Elements::Link( "?link=".$currentLink."&topicId=".$parentId."&caseId=".$case['case_id'], $case['title'], 'case' );
			$list[]		= UI_HTML_Elements::ListItem( $link, 0, array( 'id' => 'li_'.$path ) );
		}

		//  --  TEST TOPICS  --  //
		$model	= new Model_Topic();
		$model->focusIndex( 'project_id', $projectId );
		$model->focusIndex( 'parent_id', $parentId );
		$nodes	= $model->get( false );
		foreach( $nodes as $node )
		{
			$path		= $parentPath . ( $parentPath ? ".".$node['topic_id'] : $node['topic_id'] );
			$sublist	= $this->constructTree( $projectId, $currentLink, $node['topic_id'], $path, $level+1 );
			$link		= UI_HTML_Elements::Link( "?link=".$currentLink."&topicId=".$node['topic_id'], $node['title'], 'topic' );
			$list[]		= UI_HTML_Elements::ListItem( $link.$sublist, 0, array( 'id' => 'li_'.$path ) );
		}
		$attributes['class']	= "tree_".$currentLink."_".$parentId;
		if( count( $list ) )
			return UI_HTML_Elements::unorderedList( $list, $level, $attributes );
		return "";
	}
}
?>